from django.shortcuts import render
from rest_framework.response import Response
from django.contrib.auth.hashers import check_password
from rest_framework_jwt.utils import jwt_encode_handler,jwt_payload_handler
from rest_framework.views import APIView
from .models import User
from rest_framework.status import *
from utils.manag import send_message
import json

# Create your views here.
class SenSMSCode(APIView):
    def get(self,request):
        phone = request.data.get('mobile')
        resp = send_message(phone)
        resp_data = json.loads(resp)
        if resp_data['statusCode'] =="000000":
            return Response({"msg":"发送验证码成功"})
        else:
            return Response({"msg":"发送验证码失败"})
class UserCount(APIView):
    def post(self,request):
        # 登录或注册
        user = request.data.get('username')
        pwd = request.data.get('password')
        phone = request.data.get('mobile')
        # 根据user判断用户是否存在数据库中
        res =User.objects.filter(username=user).count()

        if res ==0:
            user = User.objects.create_user(username=user,password=pwd,phone=phone)
        else:
            #     登录
            user = User.objects.get(username=user)
            # 密码校验
            rest =check_password(pwd,user.password)
            if not rest:
                return Response({'msg':'账号或密码不对'},status=HTTP_204_NO_CONTENT)
        # 加密时生成第二部分的字符串
        payload =jwt_payload_handler(user)
        # 生成token
        token = jwt_encode_handler(payload)
        return Response({"username":user.username,'token':token},status=HTTP_200_OK)
#         if res > 0:
#             # 存在直接登录
#             # TODO 登录的同时记录token信息
#             return Response({'user':user},status=HTTP_200_OK)
#         # 不存在，添加到数据库中在登陆
#         else:
#             # create_user是密文存储
#             User.objects.create_user(username=user,password=pwd,phone=phone)
# #             TODO 登陆时记录token信息

# class LoginView(APIView):
#     # 先用post
#     def post(self,request):
#         # 登陆或者注册
#         user_name=request.data.get('username')
#         # 根据user_name 判断用户是否存在与数据库中
#
#         mobile=request.data.get('phone')
#         pwd=request.data.get('password')
#         res = User.objects.filter(username=user_name).count()
#
#         if res == 0:
#             # TODD 登陆的时候同时记录token信息
#             # create_user密文存储
#             user = User.objects.create_user(username=user_name,password=pwd,phone=mobile)
#             print('>>>>>>>>>>>>>>>>>>>>>>>>>',user)
#         else:
#         # 登录
#             user = User.objects.get(username=user_name)
#                 # 校验密码
#             rest = check_password(pwd,user.password)
#             if not rest:
#                 return Response({'mas':'账号和密码错误'},status=HTTP_203_NON_AUTHORITATIVE_INFORMATION)
#         # 加密时生成第二部分的字符串
#         payload_dict = jwt_payload_handler(user)
#         token = jwt_encode_handler(payload_dict)
#                 # 登陆的同时记录token的信息
#         return Response({'user':user.username,'token':token},status = HTTP_200_OK)